function PageUtils() {
};

PageUtils.getRoot = function(pages) {
  var root = {
    id: 0,
    parent_id: 0,
    title: 'Root',
    order: 0,
    _children: []
  };
  PageUtils._getRoot(root, pages);
  return root;
};

PageUtils._getRoot = function(node, pages) {
  if (!node._children) {
    node._children = [];
  }
  for (var idx in pages) {
    var page = pages[idx];
    if (page.parent_id == node.id) {
      node._children[node._children.length] = page;
      PageUtils._getRoot(page, pages);
    }
  }
};

PageUtils.getOlder = function(root, target) {
  return PageUtils._getOlder(root, target)
};

PageUtils._getOlder = function(node, target) {
  for (var idx in node._children) {
    var obj = node._children[idx];
    if (obj.id == target.id) {
      if (idx > 0) {
        return node._children[idx - 1];
      } else {
        return null;
      }
    }
    var result = PageUtils._getOlder(obj, target);
    if (result != null) {
      return result;
    }
  }
  return null;
};

PageUtils.getYounger = function(root, target) {
  return PageUtils._getYounger(root, target)
};

PageUtils._getYounger = function(node, target) {
  for (var idx in node._children) {
    var obj = node._children[idx];
    if (obj.id == target.id) {
      if (idx < (node._children.length - 1)) {
        return node._children[parseInt(idx, 10) + 1];
      } else {
        return null;
      }
    }
    var result = PageUtils._getYounger(obj, target);
    if (result != null) {
      return result;
    }
  }
  return null;
};

PageUtils.update = function(root) {
  var array = [];
  PageUtils._extractPages(root, array);
  array.sort(function(a, b) {
    return a.order - b.order;
  });
  root._children = [];
  PageUtils._getRoot(root, array);
};

PageUtils._extractPages = function(node, array) {
  for (var idx in node._children) {
    var obj = node._children[idx];
    PageUtils._extractPages(obj, array);
  }
  delete node._children;
  if (node.id != 0) {
    array.push(node);
  }
};

PageUtils.getParams = function(dir, target) {
  var result = {
    parent_id: target.parent_id,
    order: 0
  }
  if (dir == 0) {
    var older = PageUtils.getOlder(_root, target);
    if (!older) {
      result.order = target.order - 1;
    } else {
      result.order = parseFloat((target.order - older.order) / 2, 10) + parseFloat(older.order, 10);
    }
  } else {
    var younger = PageUtils.getYounger(_root, target);
    if (!younger) {
      result.order = parseFloat(target.order, 10) + 1;
    } else {
      result.order = parseFloat((younger.order - target.order) / 2, 10) + parseFloat(target.order, 10);
    }
  }
  return result;
};

PageUtils.getById = function(node, id) {
  if (node.id == id) {
    return node;
  }
  for (var idx in node._children) {
    var obj = node._children[idx];
    var result = PageUtils.getById(obj, id);
    if (result != null) {
      return result;
    }
  }
  return null;
};
